package com.ns.school.controller.edu;

import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.po.edu.LendBookDB;
import com.ns.school.service.edu.LendBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 图书借阅
 */
@Controller
public class BookBorrowController {
    @Autowired
    private LendBookService lendBookService;

    @RequestMapping("/bookBorrow/list")
    public String toList(Model model) {
        return "view/bookBorrow/list";
    }

    @RequestMapping("/bookBorrow/add")
    public String toAdd(Model model) {
        return "view/bookBorrow/add";
    }



    @RequestMapping("lendBook")
    @ResponseBody
    public LayuiResult<LendBookDB> lendBook(@RequestBody LendBookDB lendBookDB) {
        LayuiResult<LendBookDB> result = new LayuiResult<>();
        Date date = new Date();
        lendBookDB.setCreateTime(date);
        BookDB bookDB=new BookDB();
        BookDB bookDB1=new BookDB();
        bookDB.setBookId(lendBookDB.getBookId());
        bookDB1=lendBookService.findBookQty(bookDB);

        bookDB.setBookQty(bookDB1.getBookQty());
        bookDB.setLendCount(lendBookDB.getLendCount());
        if (lendBookDB.getLendCount() > bookDB.getBookQty()) {
            result.setCode(500);
            result.setMsg("借阅失败,图书数量不足");
            return result;
        } else {
            lendBookService.lendBook(result,bookDB,lendBookDB);
            return result;
        }
    }

}
